package edu.tsinghua.ddb.main.swing;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import java.util.Map.Entry;

import javax.swing.GroupLayout;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import edu.tsinghua.ddb.beans.QueryStrategy;
import edu.tsinghua.ddb.data.rowset.RowSet;
import edu.tsinghua.ddb.main.console.MyLog;
import edu.tsinghua.ddb.main.gui.QueryResult;
import edu.tsinghua.ddb.util.FileHelper;

public class Main extends JFrame {
	private static final long serialVersionUID = 1L;

	private final JPanel comparePanel = new JPanel();

	public Main() {
		super("swing gui for comparing");
		setSize(800, 600);
	}

	JTable drawTable(final RowSet rowset) {
		int countCols = rowset.getAttributes().getColumnCount();
		String[] colnames = new String[countCols];
		for (int j = 0; j < countCols; j++) {
			colnames[j] = rowset.getAttributes().getColumnName(j);
		}
		int countRows = rowset.size();
		int countRowsRender = Math.min(countRows, 200);
		Object[][] data = new Object[countRowsRender][countCols];
		for (int i = 0; i < data.length; i++) {
			for (int j = 0; j < colnames.length; j++) {
				data[i][j] = rowset.get(i, j);
			}
		}
		return new JTable(data, colnames);
	}

	public void render(final Map<QueryStrategy, QueryResult> map) {
		Vector<String> comparelist = new Vector<String>();
		comparelist.add("Query optimize strategy / elapse");
		final List<QueryResult> qrlist = new LinkedList<QueryResult>();
		qrlist.add(null);
		int i = 0;
		for (Entry<QueryStrategy, QueryResult> e : map.entrySet()) {
			comparelist.add(i++ + "#" + e.getKey() + "/"
					+ e.getValue().ellapse() + " seconds/"
					+ e.getValue().getResultCount());
			qrlist.add(e.getValue());
		}

		final Tree tree = new Tree();
		final JList list = new JList(comparelist);
		list.addListSelectionListener(new ListSelectionListener() {

			@Override
			public void valueChanged(ListSelectionEvent e) {
				try {
					if (list.getMinSelectionIndex() > 0) {
//						MyLog.debug(list.getMinSelectionIndex());
						tree.render(qrlist.get(list.getMinSelectionIndex())
								.getTree());
					}

				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
		});
		list.addMouseListener(new MouseAdapter() {
			// export the result to analyze
			@Override
			public void mouseClicked(MouseEvent e) {
				super.mouseClicked(e);
				if (e.getButton() == MouseEvent.BUTTON3) {
					String s = "";
					for (Entry<QueryStrategy, QueryResult> item : map
							.entrySet()) {
						QueryStrategy qs = item.getKey();
						if (qs.usingParallel)
							s += "1\t";
						else
							s += "0\t";
						if (qs.bottomSelect)
							s += "1\t";
						else
							s += "0\t";
						if (qs.bottomProject)
							s += "1\t";
						else
							s += "0\t";
						if (qs.joinOrder)
							s += "1\t";
						else
							s += "0\t";
						s += item.getValue().ellapse() + "\n";
					}
					try {
						FileHelper.putContent(s, "compare.txt");
					} catch (Throwable e1) {
						e1.printStackTrace();
					}
				}
			}
		});
		JScrollPane jptree = new JScrollPane(tree);
		// comparePanel.add(list);
		// comparePanel.add(tree);
		//
		GroupLayout layout = new GroupLayout(comparePanel);
		comparePanel.setLayout(layout);
		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);//
		GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
		hGroup.addGroup(layout.createParallelGroup().addComponent(list)
				.addComponent(jptree));
		layout.setHorizontalGroup(hGroup);//
		GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
		vGroup.addGroup(layout.createParallelGroup().addComponent(list));
		vGroup.addGroup(layout.createParallelGroup().addComponent(jptree));
		layout.setVerticalGroup(vGroup);
		//
		JTabbedPane tabPane = new JTabbedPane();
		tabPane.add("compare", comparePanel);
		i = 0;
		for (QueryResult qr : map.values()) {
			JTable table = drawTable(qr.getRowset());
			tabPane
					.add(i++ + "#" + qr.getResultCount(),
							new JScrollPane(table));
			add(tabPane);
		}
		setVisible(true);
	}
}
